package com.robot.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wuyunbin
 */
@Component
public class WebFilter extends ZuulFilter {

    private final Logger logger= LoggerFactory.getLogger(this.getClass());
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        logger.info("request web filter");
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String header = request.getHeader("ROBOT ");
        if (header != null && !"".equals(header)) {
            //把头信息转发下去，并且放行
            currentContext.addZuulRequestHeader("ROBOT ", header);
        }
        logger.info("web filer passed");
        return null;
    }
}